/**
 * 用户在线管理初始化
 */
var Connection = {
    id: "ConnectionTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Connection.initColumn = function () {
    return [[
            {title: '用户名', field: 'userName', visible: true, align: 'center'},
            {title: '服务器IP', field: 'serverIP', visible: true, align: 'center'},
            {title: 'AllowIP', field: 'allowIP', visible: true, align: 'center'},
            {title: '连接时间', field: 'createTime', visible: true, align: 'center'},
            {title: '是否免费线路', field: 'isFree', visible: true,
                align: 'center', templet: function (item) {
                    if (item.isFree == 0){
                        return "<label style='color:#9e3230;'>收费</label>"
                    }
                    else {
                        return "免费"
                    }
                }},
            {title: '操作', field: 'id', visible: true,
            align: 'center', templet: "<div><a onclick='offline({{d.id}})'>强制下线</a></div>"
            }
    ]];
};

function offline(itemId){
    var loading;
    layer.confirm("是否要强制用户下线",function (index) {
        layer.close(index);
        loading = layer.load(0,{
            shade:false,
            time: 30 * 1000
        });
        setTimeout(function () {
            //提交信息
            var ajax = new $ax(Feng.ctxPath + "/connection/offLine", function(data){
                Feng.success("下线成功!");
                layer.closeAll();
                Connection.table.reload("tableReload",{
                });
            },function(data){
                Feng.error("下线失败!" + data.responseJSON.message + "!");
                layer.closeAll();
            });
            ajax.set("id",itemId);
            ajax.start();
        })
    });

}


/**
 * 查询订单管理列表
 */
Connection.search = function () {
    var queryData = {};
    queryData['userName'] = $("#userName").val();
    queryData['serverType'] = $("#serverType").val();

    Connection.table.reload("tableReload",{
        page: {
            curr: 1 //重新从第1页开始
        },
        where: queryData
    });
};



Connection.tableList = function(queryData){
    layui.use("table",function () {
        var table = layui.table;

        Connection.table = table;

        table.render({
            elem: "#table",
            url: Feng.ctxPath +"/connection/list",
            cellMinWidth: 120,
            where: queryData,
            page: true,
            limit: 20,
            cols: Connection.initColumn(),
            parseData: function(res){
                return{
                    "code":0,
                    "msg":"获取成功",
                    "count":res.total,
                    "data": res.items
                }
            },
            text: {
                none: '暂无相关数据'
            },
            id: "tableReload"
        });
    });
};

$(function () {
    Connection.tableList(null);
});
